# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

# 几何形状参数
H = 10.0       # 直杆长度
D = 3.0      # 斜杆在直杆的投影长度
THETA = 45.0   # 直杆与斜杆夹角

# 大小规格（阵列数目）
X_N = 5
Y_N = 3

# 阵列间距
X_SPACE = 2 * D
Y_SPACE = 2 * (H - D / math.tan(THETA / 180.0 * math.pi))

def tan(theta):
  return math.tan(theta / 180.0 * math.pi)

# 生成胞元
def generateCellUnit():

  # 内凹六边形的六个顶点坐标（从正上方开始编号为1，逆时针编号）
  point_1 = (0, 0.5 * H - D / tan(THETA), 0)
  point_2 = (-D, 0.5 * H, 0)
  point_3 = (-D, -0.5 * H, 0)
  point_4 = (0, -(0.5 * H - D / tan(THETA)), 0)
  point_5 = (D, -0.5 * H, 0)
  point_6 = (D, 0.5 * H, 0)

  # 内凹六边形的六条边
  line_1 = (point_1, point_2)
  line_2 = (point_2, point_3)
  line_3 = (point_3, point_4)
  line_4 = (point_4, point_5)
  line_5 = (point_6, point_5)  #阵列后这里会出现重复线段，需去除
  line_6 = (point_6, point_1)
  
  # 上下两条连杆的顶点坐标
  link_point_1 = (0, H - D / tan(THETA), 0)
  link_point_2 = (0, -(H - D / tan(THETA)), 0)

  # 上下两条连杆
  link_1 = (link_point_1, point_1)
  link_2 = (link_point_2, point_4)

  lines = [line_1, line_2, line_3, line_4, line_5, line_6,
           link_1, link_2]

  return lines

# 沿x轴、y轴阵列线段
def arrayCellUnit(cellUnitLines):

  X_arrayLines = []
  Y_arrayLines = []

  # 胞元沿x轴进行阵列
  for i in range(X_N):

    for item in cellUnitLines:

      point_x_1 = (item[0][0] + X_SPACE * i, item[0][1], item[0][2])
      point_x_2 = (item[1][0] + X_SPACE * i, item[1][1], item[1][2])
      
      X_arrayLines.append((point_x_1, point_x_2))
  
  # 胞元沿y轴进行阵列
  for j in range(Y_N):

    for each in X_arrayLines:

      point_y_1 = (each[0][0], each[0][1] + Y_SPACE * j, each[0][2])
      point_y_2 = (each[1][0], each[1][1] + Y_SPACE * j, each[1][2])

      Y_arrayLines.append((point_y_1, point_y_2))
  
  # 去掉阵列后重复的线段
  Y_arrayLines = list(set(Y_arrayLines))

  return Y_arrayLines

def printParameters():

  print(" ")

  # 打印参数
  print("H: %s " % H)
  print("D: %s " % D)
  print("THETA: %s " % THETA)
  print("X_N: %d " % X_N)
  print("Y_N: %d " % Y_N)

def main():

  lines = []

  cellUnitLines = generateCellUnit()

  lines = arrayCellUnit(cellUnitLines)

  lines_tuple = tuple(lines)

  mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=DEFORMABLE_BODY)

  mdb.models['Model-1'].parts['Part-1'].WirePolyLine(mergeType=IMPRINT, meshable=ON, 
      points=lines_tuple)

  mdb.models['Model-1'].parts['Part-1'].Set(edges=
      mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#3 ]', 
      ), ), name='Wire-2-Set-1')
  
  printParameters()

main()
# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
